//
//  MyCollectionController.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/13.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYouMyCollectionController.h"
#import "MyCollectionCell.h"
#import "LYouDetailController.h"
#import "LYouVideoModel.h"

@interface LYouMyCollectionController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) NSMutableArray *allDataArray;
@property(nonatomic,strong) UIButton *backButton;
@property(nonatomic,strong) UILabel *titleLable;
@property(nonatomic,strong) UICollectionView *mainCollectionView;

@end

@implementation LYouMyCollectionController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    /** 获取收藏数据 */
    [LYouLoadingView show];
    [self gaintAllFavorData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorWithHexRGB(0x1F1E23);
    [self.view addSubview:self.backButton];
    [self.backButton setFtCornerdious:35/2 Corners:UIRectCornerTopRight|UIRectCornerBottomRight];
    [self.view addSubview:self.mainCollectionView];
    [self.view addSubview:self.titleLable];
    CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
    CGFloat hight = 25;
    if (safeBottem>0) {
        hight = 45;
    }
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(hight);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - UICollectionViewDelegate
/**分区个数*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSInteger count = self.allDataArray.count/2+self.allDataArray.count%2;
    return count;
}
/**每个分区item的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger count = self.allDataArray.count/2+self.allDataArray.count%2;
    if (section==count-1) {
        NSInteger lastCount = self.allDataArray.count - (count-1)*2;
        if (lastCount%2==0) {
            return 2;
        }else{
            return 1;
        }
    }else{
        return 2;
    }
}
/**创建cell*/
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifer = @"MyCollectionCell";
    MyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifer forIndexPath:indexPath];
    NSInteger index = indexPath.section*2+indexPath.item;
    cell.model = self.allDataArray[index];
    return cell;
}
/**点击某个cell*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了第%ldsection=%ld分item",(long)indexPath.section,indexPath.item);
    LYouDetailController *detailVC = [[LYouDetailController alloc]init];
    NSInteger index = indexPath.section*2+indexPath.item;
    detailVC.model = self.allDataArray[index];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 懒加载
-(UICollectionView *)mainCollectionView{
    if (!_mainCollectionView) {
        /**创建layout*/
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        /**设置item的行间距和列间距*/
        layout.minimumInteritemSpacing = 15;
        layout.minimumLineSpacing = 15;
        /**设置item的大小*/
        CGFloat itemW = (SCREEN_WIDTH-30-15)/2;
        layout.itemSize = CGSizeMake(itemW, 200);
        /*设置每个分区的上左下右的内边距*/
        layout.sectionInset = UIEdgeInsetsMake(15, 15,15, 15);
        /**创建collectionView*/
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 64;
        if (safeBottem>0) {
            hight = 84;
        }
        _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, hight, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.backgroundColor = ColorWithHexRGB(0x1F1E23);
        /**注册item*/
        [_mainCollectionView registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:@"MyCollectionCell"];
    }
    return _mainCollectionView;
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [UILabel new];
        _titleLable.text = @"收藏";
        _titleLable.font = LYFont_Semibold(20);
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.textColor = [UIColor whiteColor];
    }
    return _titleLable;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton new];
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 28;
        if (safeBottem>0) {
            hight = 48;
        }
        self.backButton.frame = CGRectMake(0, hight, 45, 35);
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateNormal];
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateHighlighted];
        [_backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

#pragma mark - 获取收藏数据
-(void)gaintAllFavorData{
    NSArray *allArray = [[LYVideoInfoManager share]searchAllLocalVideoData];
    self.allDataArray = [NSMutableArray arrayWithCapacity:0];
    [self.allDataArray removeAllObjects];
    for (LYouVideoInfo *info in allArray) {
        LYouVideoModel *model = [[LYouVideoModel alloc]init];
        model.post_id = info.videoId;
        model.video_url = info.videoUrl;
        model.thumbnail_url = info.videoCover;
        [self.allDataArray addObject:model];
    }
    [self.mainCollectionView reloadData];
    [LYouLoadingView hide];
}

-(void)backButtonClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
